% Mabel Zhang
% 2 Dec 2011
% CIS 520 Project

%% Find words (features) with the most word counts overall
% Eh.. didn't decide to prune top words. Decided to prune stopwords first,
%   then look at histograms after that.


% 1 x m.
count_total = sum (X, 1);

% 1 x m, sorted in decreasing word count
[count_sorted, idx_sorted] = sort (count_total, 'descend');

% Compute delta
%delta = bsxfun (@minus, count_sorted(1:end-1), count_sorted(2:end));

% threshold 1%. If count gets below 1% of the max, discard everything after it.
%   Too few words.
% (For reality of getting fewer data, might need 0.001, because 0.01 is not
%   enough data. It's good for visualizing how many words are most significant
%   for the report)
thres = 0.01;

maxWordCount = max (count_sorted);
% Find indices of elts that are > 1% of max word count
sig_idx = find (count_sorted > 0.01*maxWordCount);
% Take max index (`.` array is sorted), discard the rest of data after this
%   index
%count_sorted = count_sorted (1 : max(sig_idx));
%idx_sorted = idx_sorted (1 : max(sig_idx));
% Take top 20
n_top = 20;
count_sorted = count_sorted (1 : n_top);
idx_sorted = idx_sorted (1 : n_top);

topVocabs = cell (1, numel(idx_sorted));
% Print list of words with top word counts
for i = 1 : numel(idx_sorted)
  topVocabs {i} = vocab{idx_sorted (i)};
  disp (topVocabs (i));
  %  disp(vocab{idx_sorted (i)});
end

% Bin the sorted data
%histogram = hist (count_sorted, 20);



%% Plot bar graph
figure (1);
clf;
bar_hdl = bar (count_sorted);

set(gca,'XTick', 1:numel(topVocabs));
set(gca,'XTickLabel', topVocabs);

axis ([0 21 0 maxWordCount + 0.1*maxWordCount])




%% Color the graph

n = n_top;
colormap(summer(n));

ch = get(bar_hdl,'Children');
fvd = get(ch,'Faces');
fvcd = get(ch,'FaceVertexCData');

[zs, izs] = sortrows(count_sorted',1);

for i = 1:n
    row = izs(i);
    fvcd(fvd(row,:)) = i;
end
set(ch,'FaceVertexCData',fvcd)

k = 128;                % Number of colors in color table
colormap(summer(k));    % Expand the previous colormap
shading interp          % Needed to graduate colors
for i = 1:n
    color = floor(k*i/n);       % Interpolate a color index
    row = izs(i);               % Look up actual row # in data
    fvcd(fvd(row,1)) = 1;       % Color base vertices 1st index
    fvcd(fvd(row,4)) = 1;    
    fvcd(fvd(row,2)) = color;   % Assign top vertices color
    fvcd(fvd(row,3)) = color;
end
set(ch,'FaceVertexCData', fvcd);  % Apply the vertex coloring
set(ch,'EdgeColor','k')           % Give bars black borders


